package com.yzzrjy.controller.mainservlet.update;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzzrjy.Service.UserService;
import com.yzzrjy.util.RequestUtils;
import com.yzzrjy.util.ResponseUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;


@WebServlet(name = "UpdateUserInfoServlet", value = "/updateUserInfo")
public class UpdateUserInfoServlet extends HttpServlet {
    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uid = request.getHeader("uid");
        String token = request.getHeader("token");
        String body = RequestUtils.getBody(request);
        if (uid == null || token == null || body == null) {
            ResponseUtils.parameterError(response);
            return;
        }
        JSONObject jsonObject = JSON.parseObject(body);
        try {
            int ret = userService.updateUserInfo(uid, jsonObject);
            if(ret == 0){
                ResponseUtils.otherError(response, "内容为空");
            }else if(ret == -1){
                ResponseUtils.otherError(response, "重复的昵称");
            }else {
                ResponseUtils.success(response, "success");
            }
        } catch (SQLException e) {
            ResponseUtils.otherError(response, e.toString());
        }
    }

}
